<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use phpDocumentor\Reflection\Types\Integer;

class Goods extends Model
{


    /*
     * 采集的价格x(15-30)%作为商品的利润
     * 成本 = 采集价格-利润
     * 金额修改 SQL备份
     *UPDATE goods SET profit = ROUND(price * FLOOR(RAND() * (35 - 15 + 1) + 15) / 100)
     * */

    use HasDateTimeFormatter;
    use HasFactory;

    const TYPE_GOODS = 0; //正常商品
    const TYPE_WELFARE_GOODS = 1; //福利商品
    const TYPE_FLASH_GOODS = 2;//闪购商品

    const STATUS_NO = 0; //是否上架 0否 1是
    const STATUS_YES = 1; //是否上架 0否 1是

    const RECOMMEND_NO = 0; //是否推荐商品 0否 1是
    const RECOMMEND_YES = 1; //是否推荐商品 0否 1是
    const SPECIAL_OFFER_NO = 0; //是否特惠商品 0否 1是
    const SPECIAL_OFFER_YES = 1; //是否特惠商品 0否 1是

    const STATUS = [
        self::STATUS_NO => '下架',
        self::STATUS_YES => '上架'
    ];
    const RECOMMEND = [
        self::RECOMMEND_NO => '否',
        self::RECOMMEND_YES => '是',
    ];
    const SPECIAL_OFFER = [
        self::SPECIAL_OFFER_NO => '否',
        self::SPECIAL_OFFER_YES => '是',
    ];
    const TYPE = [
        self::TYPE_GOODS => '正常商品'
    ];

/*
    protected $fillable = [
        'category_id',
        'type',
        'name',
        'original_price',
        'price',
        'discount_rate',
        'rebate_rate',
        'rebate_integral',
        'sales',
        'virtual_sales',
        'quality',
        'img',
        'detail',
        'status',
        'recommend',
        'special_offer',
        'wind_control',
        'updated_at'
    ];
*/
    public function category()
    {
        return $this->belongsTo(GoodsCategory::class);
    }

    public function shopwindows(){
        return $this->hasMany(Shopwindow::class);
    }

    public function ads(){
        return $this->hasMany(Ad::class);
    }

    public function setImgsAttribute($imgs)
    {
        if (is_array($imgs)) {
            $this->attributes['imgs'] = json_encode($imgs);
        }
    }

    public function getImgsAttribute($imgs)
    {
        return json_decode($imgs, true);
    }


    /**
     * 获取商品列表
     * @param $where array 查询条件
     * @param $page int 页码
     * @param $limit int 每页条数
     * @param $orderBy array 排序
     * @return Builder[]|Collection
     */
    public static function getList(array $where, int $page, int $limit, array $orderBy = [], array $with = [],array $whereIn = [])
    {
        $model = self::query()
            ->whereHas('category', function ($query) {
                $query->where('status', GoodsCategory::STATUS_YES);
            });
        if (!empty($with)) {
            $model->with($with);
        }
        if (!empty($where)) {
            $model->where($where);
        }
        if (!empty($whereIn)) {
            $model->whereIn($whereIn[0],$whereIn[1]);
        }
        if (!empty($page) && $page > 0) {
            $model->offset($page);
        }
        if (!empty($orderBy)) {
            $model->orderBy($orderBy[0], $orderBy[1]);
        }
        return $model->limit($limit)
            ->orderBy('id', 'desc')
            ->select('id','code', 'category_id', 'name', 'original_price', 'price','profit', 'img','quality')
            ->get();
    }

    /**
     * 商品详情
     * @param $goods_id
     * @return Builder|Model|object|null
     */
    public static function goodsDetail($goods_id)
    {
        /*
        return self::query()
            ->where('id', $goods_id)
            ->where('status', self::STATUS_YES)
            ->whereHas('category', function ($query) {
                $query->where('status', GoodsCategory::STATUS_YES);
            })
            ->select('id', 'type', 'category_id', 'name', 'original_price', 'price', 'discount_rate', 'label', 'tag', 'rebate_rate',
                'rebate_integral', 'virtual_sales', 'quality', 'img', 'imgs', 'detail','allcart','allwish')
            ->first();
        */
    }

    /**
     * 获取指定商品
     * @param array $goods_id
     * @return Goods[]|Builder[]|Collection|\LaravelIdea\Helper\App\Models\_IH_Goods_C|\LaravelIdea\Helper\App\Models\_IH_Goods_QB[]
     */
    public static function getDesignatedGoods(array $goods_id)
    {

        return self::query()
            ->whereIn('id', $goods_id)
            ->where('status', self::STATUS_YES)
            ->whereHas('category', function ($query) {
                $query->where('status', GoodsCategory::STATUS_YES);
            })
            ->select('id', 'category_id', 'name', 'original_price', 'price', 'profit', 'img', 'quality')
            ->get();

    }



    //订单详情
    public function goodsOrderDetail(): HasMany
    {
        return $this->hasMany(GoodsOrderDetail::class);
    }


    //回滚库存
    public static function rollbackQuality($order_detail)
    {
        //$goods = Goods::query()->where('id', $order_detail->goods_id)->first();
        if (!empty($goods)) {
            //$goods->increment('quality', $order_detail->number);
            //$goods->decrement('sales', $order_detail->number);
        }
    }


    public static function getGoods(){
        $goods = self::query()->pluck('name','id')->toArray();
        return $goods;
    }


}
